# coding=utf-8
import unittest
import random

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

from src.common.screenshot import *


class JyyTest (unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox ()
        self.driver.implicitly_wait (15)
        self.num = random.randint (10000, 99999)  # 随机生成手机号后五位
        self.certNo = "913205835985959259"  # 统一社会信用代码
        self.companyName = "昆山福人居房产咨询有限公司"  # 企业名称
        self.url = "https://jr.fangdd.net/shangdd/login"

    def tearDown(self):
        self.driver.close ()  # 关闭当前窗口
        self.driver.quit ()  # 退出驱动程序并关闭所有相关窗口

    def jyy_login(self):
        '''金蚁云登录测试'''
        self.driver.get (self.url)
        # self.driver.set_window_size (764, 672)
        self.driver.maximize_window ()
        time.sleep (2)
        self.driver.find_element (By.ID, "userTel").click ()
        element = self.driver.find_element (By.ID, "userTel")
        actions = ActionChains (self.driver)
        actions.double_click (element).perform ()
        self.driver.execute_script ("window.scrollTo(0,0)")
        self.driver.find_element (By.ID, "userTel").send_keys ("13058019302")
        self.driver.find_element (By.ID, "password").click ()
        self.driver.find_element (By.ID, "password").send_keys ("111111")
        self.driver.find_element (By.CSS_SELECTOR, ".ant-btn-link").click ()
        self.driver.find_element (By.ID, "verifyCode").click ()
        self.driver.find_element (By.ID, "verifyCode").send_keys ("1234")
        time.sleep (2)
        self.driver.find_element (By.CSS_SELECTOR, ".\\_2E8mu").click ()
        time.sleep (2)
        takeScreenshot (self.driver, createDir ("jyy"), "login")  # 调用截图方法截图执行后页面并保存执行结果页面

    def test_company_add(self):
        '''添加成员企业测试'''
        self.jyy_login ()
        self.driver.find_element (By.XPATH, "//*[@id='1$Menu']/li[2]/a").click ()
        self.driver.find_element (By.XPATH, "//*[@id='root']/section/section/div/main/div/div/a").click ()
        takeScreenshot (self.driver, createDir ("jyy"), "company_add")
        # 添加成员企业信息页面
        self.driver.find_element (By.ID, "companyName").click ()
        self.driver.find_element (By.ID, "companyName").send_keys (self.companyName)
        self.driver.find_element (By.ID, "certNo").click ()
        self.driver.find_element (By.ID, "certNo").send_keys (self.certNo)

        self.driver.find_element (By.CSS_SELECTOR, "#companyType .ant-select-selection__placeholder").click ()
        time.sleep (2)
        self.driver.find_element (By.CSS_SELECTOR, ".ant-select-dropdown-menu-item-active").click ()

        self.driver.find_element (By.CSS_SELECTOR, "#companyLineType .ant-select-selection__placeholder").click ()
        time.sleep (2)
        self.driver.find_element (By.CSS_SELECTOR, ".ant-select-dropdown-menu-item-active").click ()

        self.driver.find_element (By.ID, "legalName").click ()
        self.driver.find_element (By.ID, "legalName").send_keys ("法人姓名")
        self.driver.find_element (By.ID, "legalCardNo").click ()
        self.driver.find_element (By.ID, "legalCardNo").send_keys ("411303198908095223")

        self.driver.find_element (By.CSS_SELECTOR, "#legalExpDate .ant-calendar-picker-input").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-selected-day > .ant-calendar-date").click ()

        self.driver.find_element (By.ID, "legalTel").click ()
        self.driver.find_element (By.ID, "legalTel").send_keys ("130500%s" % self.num)
        self.driver.find_element (By.ID, "bankNo").click ()
        self.driver.find_element (By.ID, "bankNo").click ()
        self.driver.find_element (By.ID, "bankNo").send_keys ("6214856551133309")
        self.driver.find_element (By.ID, "bankName").click ()
        self.driver.find_element (By.ID, "bankName").send_keys ("深圳科苑支行")
        self.driver.find_element (By.ID, "bankCode").click ()
        self.driver.find_element (By.ID, "bankCode").send_keys ("105302000015")
        self.driver.find_element (By.ID, "handerName").click ()
        self.driver.find_element (By.ID, "handerName").send_keys ("test01")
        self.driver.find_element (By.ID, "handerCardNo").click ()
        self.driver.find_element (By.ID, "handerCardNo").send_keys ("411303198908095327")

        self.driver.find_element (By.CSS_SELECTOR, "#handerExpDate .ant-calendar-picker-input").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR,
                                  "tr:nth-child(3) > .ant-calendar-cell:nth-child(6) > .ant-calendar-date").click ()

        self.driver.find_element (By.ID, "handerTel").click ()
        self.driver.find_element (By.ID, "handerTel").send_keys ("130570%s" % self.num)
        self.driver.find_element (By.ID, "authName").click ()
        self.driver.find_element (By.ID, "authName").send_keys ("test02")
        self.driver.find_element (By.ID, "authCardNo").click ()
        self.driver.find_element (By.ID, "authCardNo").click ()
        self.driver.find_element (By.ID, "authCardNo").send_keys ("411303198908091811")

        self.driver.find_element (By.CSS_SELECTOR, "#authExpDate .ant-calendar-picker-input").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR, ".ant-calendar-next-year-btn").click ()
        self.driver.find_element (By.CSS_SELECTOR,
                                  "tr:nth-child(5) > .ant-calendar-cell:nth-child(5) > .ant-calendar-date").click ()

        self.driver.find_element (By.ID, "authTel").click ()
        self.driver.find_element (By.ID, "authTel").send_keys ("130560%s" % self.num)
        time.sleep (2)
        takeScreenshot (self.driver, createDir ("jyy"), "company_add_info")

        self.driver.find_element (By.CSS_SELECTOR, ".ant-btn-primary").click ()
        time.sleep (5)
        takeScreenshot (self.driver, createDir ("jyy"), "company_add_result")


if __name__ == '__main__':
    a = JyyTest ()
    a.test_jyy ()
